package com.bang.concurrency.example.rateLimiter;

import com.google.common.util.concurrent.RateLimiter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @Auther: Bang
 * @Date: 2019/10/17 22:25
 * @Description: https://www.jianshu.com/p/5d4fe4b2a726
 */
@Slf4j
public class RateLimiterExample1 {

    private static RateLimiter rateLimiter = RateLimiter.create(5);

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            if (rateLimiter.tryAcquire(190, TimeUnit.MILLISECONDS)){
                handle(i);
            }
        }
    }

    private static void handle(int i) {
        log.error("{}",i);
    }
}
